﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using ZhongKeSite.Common;
using ZhongKeSite.Common.Helper;
using ZhongKeSite.Entity;
using ZhongKeSite.Service;
using ZhongKeSite.Web.Models;

namespace ZhongKeSite.Web.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    public class SysController : BaseController
    {
        ISysService sysService;
        public SysController(ISysService _sysService)
        {
            sysService = _sysService;
        }

        #region 用户管理
        [RoleAuthorize("UserList")]
        public IActionResult UserList(string search = "", int pageindex = 1, int pagesize = 10)
        {
            try
            {
                if (Request.IsAjaxRequest())
                {
                    return Json(sysService.GetSysUserPagedList(pageindex, pagesize, search));
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
            return View();
        }

        /// <summary>
        /// 系统用户添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("UserAdd")]
        public IActionResult UserAdd()
        {
            try
            {
                SysUser model = new SysUser();
                ViewBag.RoleDLL = sysService.GetSysRoleListDLL();
                return View(model);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }

        /// <summary>
        /// 系统用户添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("UserAdd")]
        public IActionResult UserAdd(SysUser model)
        {
            try
            {
                model.AddBy = CurrentUser.Login;
                return Json(sysService.AddSysUser(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("UserStart")]
        public IActionResult UserStart(List<string> ids)
        {
            try
            {
                return Json(sysService.UpdateSysUserState(ids, CurrentUser.Login, DataStatus.IsAvailable));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        /// <summary>
        /// 禁用
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("UserStop")]
        public IActionResult UserStop(List<string> ids)
        {
            try
            {
                return Json(sysService.UpdateSysUserState(ids, CurrentUser.Login, DataStatus.UnAvailable));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("UserDel")]
        public IActionResult UserDel(List<string> ids)
        {
            try
            {
                return Json(sysService.UpdateSysUserState(ids, CurrentUser.Login, DataStatus.Deleted));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        /// <summary>
        /// 系统用户编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("UserEdit")]
        public IActionResult UserEdit(string id)
        {
            try
            {
                ViewBag.RoleDLL = sysService.GetSysRoleListDLL();
                return View();
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 系统用户编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("UserEdit")]
        public IActionResult UserEdit(SysUser model)
        {
            try
            {
                model.UpdateBy = CurrentUser.Login;
                return Json(sysService.UpdateSysUser(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        #endregion

        #region 角色管理
        [RoleAuthorize("RoleList")]
        public IActionResult RoleList(int pageindex = 1, int pagesize = 10)
        {
            try
            {
                if (Request.IsAjaxRequest())
                {
                    return Json(sysService.GetSysRolePagedList(pageindex, pagesize));
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
            return View();
        }

        [RoleAuthorize("RoleAdd")]
        public IActionResult RoleAdd()
        {
            try
            {
                ViewBag.ParentRoleDLL = sysService.GetSysRoleDLL(CommonConst.All);
                return View();
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }

        /// <summary>
        /// 系统角色添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("RoleAdd")]
        public IActionResult RoleAdd(SysRole model)
        {
            try
            {
                model.AddBy = CurrentUser.Login;
                return Json(sysService.AddSysRole(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        /// <summary>
        /// 系统角色权限设置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("RoleSetup")]
        public ActionResult RoleSetup(string id)
        {
            List<Dynatree> model = new List<Dynatree>();
            try
            {
                model = sysService.GetRoleRightsTree(id);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }
            return View(model);
        }
        /// <summary>
        /// 系统角色权限设置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("RoleSetup")]
        public ActionResult RoleSetup(SettingRequest model)
        {
            try
            {
                return Json(sysService.RoleSetup(model, CurrentUser));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        /// <summary>
        /// 系统角色编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("RoleEdit")]
        public ActionResult RoleEdit(string id)
        {
            try
            {
                ViewBag.ParentRoleDLL = sysService.GetSysRoleDLL(id);
                return View();
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 系统角色编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("RoleEdit")]
        public ActionResult RoleEdit(SysRole model)
        {
            try
            {
                model.UpdateBy = CurrentUser.Login;
                return Json(sysService.UpdateSysRole(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        /// <summary>
        /// 系统角色删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("RoleDel")]
        public ActionResult RoleDel(string id)
        {
            try
            {
                return Json(sysService.DeleteSysRole(id));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }
            return Json(new { success = false, msg = "保存失败!请重试." });
        }
        #endregion

        #region SysRight 权限控制
        [HttpGet, HttpPost]
        [RoleAuthorize("SysRightList")]
        public ActionResult SysRightList(string id)
        {
            try
            {
                SysRight model = new SysRight();
                if (!string.IsNullOrEmpty(id))
                {
                    model = sysService.GetSysRight(id);
                }

                if (Request.Method.EqualsIgnoreCase("GET"))
                {
                    ViewBag.Title = ViewBag.Title + " -> " + model.RightName;
                    List<Dynatree> treeList = sysService.GetRoleRightsTree(CommonConst.Root);
                    ViewBag.TreeList = treeList;
                    List<SelectListItem> sysDictCodeDLL = sysService.GetSysDictCodeDLL("ActionType", false, false, new SelectListItem { Text = "--未定义--", Value = "" });
                    ViewBag.SysDictCodeDLL = sysDictCodeDLL;
                    return View(model);
                }
                else
                {
                    return Json(new { success = true, data = model });
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }

        [HttpPost]
        [RoleAuthorize("SysRightEdit")]
        public ActionResult SysRightEdit(SysRight model)
        {
            try
            {
                model.UpdateBy = CurrentUser.Login;
                return Json(sysService.UpdateSysRight(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        [HttpPost]
        [RoleAuthorize("SysRightAdd")]
        public ActionResult SysRightAdd(SysRight model)
        {
            try
            {
                model.AddBy = CurrentUser.Login;
                return Json(sysService.AddSysRight(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        [HttpPost]
        [RoleAuthorize("SysRightDel")]
        public ActionResult SysRightDel(SysRight model)
        {
            try
            {
                return Json(sysService.DeleteSysRight(model.ID));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        #endregion
    }
}